use dora_node_api::{self, dora_core::config::DataId, DoraNode, Event, IntoArrow};
use input::{get_image_bytes, Source};
use std::env;
use std::str::FromStr;

fn main() -> eyre::Result<()> {
    let source = env::var("source")?;
    let mut source = Source::from_str(source.as_str())?;

    //输出
    let data_id = DataId::from("image".to_owned());
    let (mut node, mut events) = DoraNode::init_from_env()?;
    while let Some(event) = events.recv() {
        match event {
            Event::Input {
                id,
                mut metadata,
                data: _,
            } => match id.as_str() {
                "tick" => {
                    let image = get_image_bytes(&mut source)?;
                    node.send_output(
                        data_id.clone(),
                        metadata.parameters.clone(),
                        image.into_arrow(),
                    )?;
                    match source {
                        Source::FILE(_) => { break }
                        Source::HTTP(_) => { break }
                        Source::RTSP(_, _, _) => { continue }
                        Source::CAMERA(_, _, _) => { continue }
                    }
                }
                other => eprintln!("Ignoring unexpected input `{other}`"),
            },
            Event::Stop => println!("Received manual stop"),
            other => eprintln!("Received unexpected input: {other:?}"),
        }
    }

    println!("finished");
    Ok(())
}
